// deepclone/clone
// create/new/instanceof
// 对象比较/对象是否有循环引用
// object value可迭代
// object 实现for of 可迭代




Object.myCreate = function(obj){
    function fn (){}
    fn.prototype = obj
    return new fn()
}
console.log(Object.myCreate({a:1}))
console.log(Object.create({a:1}))


var myNew = function(Obj, ...args){
    let newfn = Object.create(Obj.prototype)
    let res = newfn.call(this, ...args)
    return res instanceof Object ? res : newfn
}

var myInstanceOf = function(obj, Obj){
    let p1 = Object.getPrototypeOf(obj)
    let p2 = Obj.prototype
    while(true){
        if(p1 === null) return false
        if(p1 === p2) return true
        p1 = Object.getPrototypeOf(p1)
    }
}
// 示例
function A() {}
let a1 = new A();

console.log(myInstanceOf(a1, A), '---'); // true
console.log(myInstanceOf(a1, Object)); // true
console.log(myInstanceOf(a1, Array)); // false


// const [a, b] = {a: 1, b:2} 等式可执行
Object.prototype[Symbol.iterator] = function () {
    return Object.values(this)[Symbol.iterator]()
}
const [a, b] = { a: 1, b: 2 }
console.log(a, b, '----')




var myIterator = {
    data: [1, 2, 3, 4],
    [Symbol.iterator](){
        let index = 0;
        return {
            next: () =>{
                return {
                   done: index >= this.data.length,
                   value: this.data[index++],
                }
            }
        }
    }
}
for (let val of myIterator) {
    console.log(val, "??")
}
console.log([...myIterator])


function hasCycle(obj, map = new WeakMap){
    if(obj === null || typeof obj !== 'object'){
        return false
    }
    if(map.has(obj)){
        return true
    }
    map.set(obj, true)

    for(let key in obj){
        if(obj.hasOwnProperty(key)){
            const value = obj[key];
            if(typeof value === 'object' && hasCycle(value, map)){
                return true
            }
        }
    }
    return false
}
// 示例  
const aa = {c: 1};  
const bb = { aa };  
aa.bb = bb; // 创建循环依赖  
  
console.log(hasCycle(aa), 'hascycle'); // 输出: true  


function isObject(obj) {
    return obj !== null && typeof obj === 'object';
}

function deepEqual(obj1, obj2) {
    // 处理不同类型的比较
    if (obj1 === obj2) return true;
    // 处理一个为 null 或 undefined，另一个不是的情况
    if (!isObject(obj1) || !isObject(obj2)) return false;
    // 获取对象的键
    const keys1 = Object.keys(obj1);
    const keys2 = Object.keys(obj2);
    // 如果键的数量不同，则对象不相等
    if (keys1.length !== keys2.length) return false;
    // 检查每个键对应的值是否相等
    for (let key of keys1) {
        if (!keys2.includes(key)) {
            return false;
        }
        //  hanshu
        if (typeof [obj1][key] === 'function' && typeof [obj2][key] === 'function') {
            x = [obj1][key].toString();
            y = [obj2][key].tostring();
            return x === y
        } else {
            if (!deepEqual(obj1[key], obj2[key])) {
                return false;
            }
        }
    }
    return true;
}



// 测试用例
const objA = { a: 1, b: { c: 2 } };
const objB = { a: 1, b: { c: 2 } };
const objC = { a: 1, b: { c: 3 } };

console.log(deepEqual(objA, objB)); // true
console.log(deepEqual(objA, objC)); // false